/**
 * Copyright 2006 Bosco Curtu
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * 
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
 * either express or implied. 
 * 
 * See the License for the specific language governing permissions and limitations 
 * under the License.
*/
package com.bcurtu.test;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import junit.framework.Assert;
import junit.framework.TestCase;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.bcurtu.db.Account;
import com.bcurtu.db.AccountDao;
import com.bcurtu.james.JamesManager;


public class MailForwarder extends TestCase {

	public static final String CONTEXT_CONFIG_LOCATION = "bin/applicationContext.xml";
	public static final String CONTEXT_DB_CONFIG_LOCATION = "bin/applicationContext-db-test.xml";
	public static final String CONTEXT_JAMES_CONFIG_LOCATION = "bin/applicationContext-james.xml";

	private JamesManager james;
	private AccountDao accManager;
	private Logger log =Logger.getLogger(this.getClass());
	 

	protected void setUp(){
		ApplicationContext context = new FileSystemXmlApplicationContext(new String[]{
				CONTEXT_CONFIG_LOCATION,
				CONTEXT_JAMES_CONFIG_LOCATION,
				CONTEXT_DB_CONFIG_LOCATION
		});
		james = (JamesManager) context.getBean("JamesManager");
		accManager = (AccountDao) context.getBean("AccountManager");
	}

	public void testCreateDeleteJames() throws Exception{
		
		String alias="uno";
		String forward="forward@gmail.com";
		
		Assert.assertEquals("verifyFreeAccount "+alias,true,james.verifyFreeAccount(alias));
		//Assert.assertEquals("verifyFreeAccount "+alias,false,james.verifyExistingAccount(alias));
		
		james.createAccount(alias,forward);

		//Assert.assertEquals("verifyFreeAccount "+alias,false,james.verifyFreeAccount(alias));
		Assert.assertEquals("verifyFreeAccount "+alias,true,james.verifyExistingAccount(alias));
		
		james.deleteAccount(alias);

		Assert.assertEquals("verifyFreeAccount "+alias,true,james.verifyFreeAccount(alias));
	//	Assert.assertEquals("verifyFreeAccount "+alias,false,james.verifyExistingAccount(alias));
	}
	
	public void testCreateDeleteDB() throws Exception{
		
		String alias="uno";
		String forward="forward@gmail.com";

		Assert.assertEquals("Is valid alias",true,accManager.isValidAlias(alias));

		Account acc=new Account();
		acc.setAlias(alias);
		acc.setForward(forward);
		acc.setActive(Boolean.TRUE);
		Calendar c=Calendar.getInstance();
		c.setTime(new Date());
		acc.setBlockingDate(c.getTime());
		acc.setExpirationDate(c.getTime());

		accManager.insertAccount(acc);
		Assert.assertEquals("Is valid alias",false,accManager.isValidAlias(alias));

		accManager.blockAccount(acc.getAlias());
		Assert.assertEquals("Is valid alias",false,accManager.isValidAlias(alias));

		int i=accManager.deleteExpiredAccounts(c.getTime());
		Assert.assertTrue("At least an account has expired",i>0);
		Assert.assertEquals("Is valid alias",true,accManager.isValidAlias(alias));


	}
	
	public void testCreateNotExpiringAccounts() throws Exception{

		String alias="dos";
		String forward="forward@gmail.com";

		Assert.assertEquals("Is valid alias",true,accManager.isValidAlias(alias));

		Account acc=new Account();
		acc.setAlias(alias);
		acc.setForward(forward);
		acc.setActive(Boolean.TRUE);
		Calendar c=Calendar.getInstance();
		c.setTime(new Date());
		c.add(Calendar.MINUTE,10);
		acc.setBlockingDate(c.getTime());
		acc.setExpirationDate(c.getTime());

		accManager.insertAccount(acc);
		Assert.assertEquals("Is valid alias",false,accManager.isValidAlias(alias));

		List<Account> accs=accManager.getBlockingAccounts(new Date());
		for (Account a:accs){
			accManager.blockAccount(a.getAlias());
		}
		Assert.assertEquals("Is valid alias",false,accManager.isValidAlias(alias));

//		accManager.deleteExpiredAccounts();
//		Assert.assertEquals("Is valid alias",false,accManager.isValidAlias(alias));

		accManager.forceDeleteAccount(acc.getAlias());
		Assert.assertEquals("Is valid alias",true,accManager.isValidAlias(alias));

	}

//	public void testCreateDB() throws Exception{
//		
//		String alias_root="user";
//		String forward="forward@gmail.com";
//
//		for (int i=0;i<10;i++){
//			String alias=alias_root+i;
//			log.debug("Inserting NEW alias="+alias);
//			log.debug("Is valid alias="+accManager.isValidAlias(alias));
//			Assert.assertEquals("Is valid alias",true,accManager.isValidAlias(alias));
//	
//			Account acc=new Account();
//			acc.setAlias(alias);
//			acc.setForward(forward);
//			acc.setActive(Boolean.TRUE);
//			Calendar c=Calendar.getInstance();
//			c.setTime(new Date());
//			c.add(Calendar.YEAR,1);
//			acc.setBlockingDate(c.getTime());
//			acc.setExpirationDate(c.getTime());
//	
//			log.debug("Inserting...");
//			accManager.insertAccount(acc);
//			log.debug("Is valid alias now="+accManager.isValidAlias(alias));
//			Assert.assertEquals("Is valid alias",false,accManager.isValidAlias(alias));
//		}
//	}

}
